﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using whatpricemyhouse.LINQ;

namespace whatpricemyhouse.agent
{
    public partial class EmailPreview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/Agent/BuyerList.aspx?old=1");
            }
            else
            {
                if (!String.IsNullOrEmpty(Request.QueryString["emailid"]))
                {
                    String emailId = Request.QueryString["emailid"];
                    LoadEmail(emailId);
                }
                else
                {
                    Response.Redirect("~/Agent/BuyerList.aspx?old=1");
                }
            }
        }

        String email = "";
        private void LoadEmail(string emailId)
        {
            var emails = from e in dcdc.email_tables
                         where e.EmailID == emailId
                         select e;
            int i = 0;
            foreach (var email in emails)
            {
                Response.Write(email.email_body);
                deleteEmail(emailId);
                i++;
            }
            if (i == 0)
            {
                Response.Redirect("~/Agent/BuyerList.aspx?old=1");
            }
        }

        DataControllerDataContext dcdc = new DataControllerDataContext();
        private void deleteEmail(String emailId)
        {
            var showList = from s in dcdc.email_show_property_list_tables
                           where s.EmailID == emailId
                           select s;
            foreach (var show in showList)
            {
                dcdc.email_show_property_list_tables.DeleteOnSubmit(show);
            }

            var emailList = from e in dcdc.email_tables
                            where e.EmailID == emailId
                            select e;

            foreach (var email in emailList)
            {
                dcdc.email_tables.DeleteOnSubmit(email);
            }
            dcdc.SubmitChanges();
        }
    }
}